package com.example.springboot_jdk8;

import com.example.springboot_jdk8.config.ReloadDefaultListableBeanFactory;
import com.example.springboot_jdk8.config.ScanBasePackage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication(scanBasePackages = { "com.example"})
public class SpringbootJdk8Application {

    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(SpringbootJdk8Application.class, args);

        //在 AnnotationConfigServletWebApplicationContext 构造方法中,会设置扫描包路径,并且 refresh()
        //新容器
        ReloadDefaultListableBeanFactory reloadDefaultListableBeanFactory = new ReloadDefaultListableBeanFactory();
        //将新容器放入上下文环境
        AnnotationConfigServletWebApplicationContext annotationConfigServletWebApplicationContext = new AnnotationConfigServletWebApplicationContext(reloadDefaultListableBeanFactory);
        //设置当前上下文的父级,以及新容器的父容器
        annotationConfigServletWebApplicationContext.setParent(run);
        //将启动类注册进新容器,启动类会作为配置项以 scanBasePackages = { "com.example"} 为基础,扫描新的bean
        annotationConfigServletWebApplicationContext.register(ScanBasePackage.class);
        annotationConfigServletWebApplicationContext.refresh();
        //子容器
        Object testBean = annotationConfigServletWebApplicationContext.getBean("testBean2");
        System.out.println(testBean);
        //父容器
        Object testBean1 = annotationConfigServletWebApplicationContext.getParent().getBean("testBean2");
        Object testBean2 = annotationConfigServletWebApplicationContext.getParent().getBean("testBean");
        System.out.println(testBean1);
        System.out.println(testBean2);
    }

}
